﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.JinShengService;
using System.Collections.ObjectModel;
using CommunitySystem.Ctrls;

namespace CommunitySystem.PageView.晋升管理.新的晋升管理
{
    public partial class JSControl4 : UserControl
    {
        private JinShengService.JinShengServiceClient _client = null;
        private JinShengService.JinShen_ChooseCandidator_Main chooseMain = null;
        private JinShengService.JinShen_MainInfo _MainInfo = null;
        private ObservableCollection<JinShengService.JinShen_PublicShow_Detail> details = new ObservableCollection<JinShengService.JinShen_PublicShow_Detail>();

        private string Role = "";
        private bool CanEditInfo = true;
        public JSControl4(JinShen_MainInfo mainInfo, ObservableCollection<JinShengService.JinShen_PublicShow_Detail> _details,string role)
        {
            InitializeComponent();
            _MainInfo = mainInfo;
          //  details.Clear();
            details = _details;
            Role = role;
            LoadUI();
        }
      

        private void LoadUI()
        {
            var des = details.Where(p => p.MainID == _MainInfo.ID).ToList();
            if (des != null && des.Count > 0)
            {
                TextBlock tb = new TextBlock() { Text = "科室:" + _MainInfo.Department, Margin = new Thickness(10, 0, 0, 0), VerticalAlignment = System.Windows.VerticalAlignment.Center };
                selectionInfo.Children.Add(tb);
                tb = new TextBlock() { Text = "职位:" + _MainInfo.Position + "(" + _MainInfo.PositionLevel + ")", Margin = new Thickness(60, 0, 0, 0), VerticalAlignment = System.Windows.VerticalAlignment.Center };
                selectionInfo.Children.Add(tb);
            foreach (var d in des)
            {
                Border b = new Border() { BorderBrush = new SolidColorBrush(Colors.Blue), BorderThickness = new Thickness(1), Margin = new Thickness(5) };
                StackPanel sp = new StackPanel() { Margin = new Thickness(3), DataContext = d };
                b.Child = sp;
                StackPanel sp1 = new StackPanel() { Orientation = Orientation.Horizontal, Margin = new Thickness(3) };
                sp.Children.Add(sp1);

                 tb = new TextBlock() { Text = "经", Margin = new Thickness(0, 3, 0, 0) };
                sp1.Children.Add(tb);
                DatePicker dp = new DatePicker() { Margin = new Thickness(5, 0, 5, 0) };
               
                dp.IsEnabled = CanEditInfo;
                dp.SetBinding(DatePicker.SelectedDateProperty, new System.Windows.Data.Binding("StartDate") { Mode = System.Windows.Data.BindingMode.TwoWay, StringFormat = "yyyy-MM-dd" });
                sp1.Children.Add(dp);
                tb = new TextBlock() { Text = "至", Margin = new Thickness(0, 3, 0, 0) };
                sp1.Children.Add(tb);
                dp = new DatePicker() { Margin = new Thickness(5, 0, 5, 0) };
                //if (CanEditInfo)
                dp.IsEnabled = CanEditInfo;
                dp.SetBinding(DatePicker.SelectedDateProperty, new System.Windows.Data.Binding("EndDate") { Mode = System.Windows.Data.BindingMode.TwoWay, StringFormat = "yyyy-MM-dd" });
                sp1.Children.Add(dp);

                var dgwxx = _MainInfo.DangGongWei_Detail.Where(p => p.MainID == d.MainID).Where(p=>p.EmpID==d.EmpID).First();
                // tb = new TextBlock() { Text = "公示" + d.EmpName + "同志晋升" + _mainInfo.Position + "(" + _mainInfo.PositionLevel + ")", Margin = new Thickness(0, 3, 5, 0) };
                tb = new TextBlock() { Text = "公示" + d.EmpName + "同志晋升" + dgwxx.Position + "(" + dgwxx.AttributLevel + ")", Margin = new Thickness(0, 3, 5, 0) };
                sp1.Children.Add(tb);

                if (string.IsNullOrEmpty(d.Condition))
                {
                    d.Condition = "未收到情况反映";
                }
                DictComboBox dict = new DictComboBox() { Field = "Condition", DictCode = "HasProblem", DisplayMemberPath = "DictValue", SelectedValuePath = "DictValue", Width = 150 };
                sp1.Children.Add(dict);

                if (_MainInfo.PromotType.Equals("选举"))
                {
                    Button btxjjg = new Button() { Content = "选举结果", Margin = new Thickness(60, 0, 20, 0) };
                    btxjjg.Click += (sender11, e11) =>
                    {
                        XJJGWindow xjjg = new XJJGWindow(_MainInfo, d,Role);
                        xjjg.Show();
                        xjjg.Closed += (sender, e) => { };
                    };
                    sp1.Children.Add(btxjjg);
                }
                // if (CanEditInfo)
                dict.IsEnabled = CanEditInfo;
                StackPanel sp2 = new StackPanel() { Orientation = Orientation.Horizontal, Margin = new Thickness(3), DataContext = d };
                tb = new TextBlock() { Text = "反映情况:", Margin = new Thickness(0, 3, 0, 0) };
                sp2.Children.Add(tb);

                var box1 = new TextBox() { Width = 150, DataContext = d };
                box1.IsEnabled = CanEditInfo;
                box1.SetBinding(TextBox.TextProperty, new System.Windows.Data.Binding("FanYinQingkuang") { Mode = System.Windows.Data.BindingMode.TwoWay });
                sp2.Children.Add(box1);

                dict.Tag = sp2;
                dict.SelectionChanged += (sender, e) =>
                {
                    var com = sender as DictComboBox;
                    var panel = com.Tag as StackPanel;
                    panel.Visibility = System.Windows.Visibility.Collapsed;
                    d.IsAgree = true;
                    var item = dict.SelectedItem as DataService.DataDictionaryItemModel;
                    if (item.DictValue.Equals("有情况反映"))
                    {
                        panel.Visibility = System.Windows.Visibility.Visible;
                        d.IsAgree = false;
                    }
                    else
                    {
                        panel.Visibility = System.Windows.Visibility.Collapsed;
                        d.IsAgree = true;
                    }
                };
                tb = new TextBlock() { Text = "处理意见如下 : ", Margin = new Thickness(10, 3, 0, 0) };
                sp2.Children.Add(tb);
                TextBox box = new TextBox() { Width = 150 };
                // if (CanEditInfo)
                box.IsEnabled = CanEditInfo;
                box.SetBinding(TextBox.TextProperty, new System.Windows.Data.Binding("DSC") { Mode = System.Windows.Data.BindingMode.TwoWay });


                tb = new TextBlock() { Text = "是否同意晋升 : ", Margin = new Thickness(10, 3, 0, 0) };
                var dict1 = new ComboBox() { DisplayMemberPath = "DictValue", SelectedValuePath = "DictCode", Width = 150, DataContext = d };
                dict1.ItemsSource = CommunitySystem.Common.InitialDictionary._Dicts["IsAgreePromot"].Item;
                if (d.IsAgree == true)
                {
                    dict1.SelectedIndex = 0;
                    d.IsAgree = true;
                }
                else
                {
                    dict1.SelectedIndex = 1;
                    d.IsAgree = false;
                }
                dict1.SelectionChanged += (sender, e) =>
                {
                    if (dict1.SelectedIndex == 0)
                    {
                        d.IsAgree = true;
                    }
                    else
                    {
                        d.IsAgree = false;
                    }
                };
                dict1.IsEnabled = CanEditInfo;
                sp2.Children.Add(box);

                sp2.Children.Add(tb);
                sp2.Children.Add(dict1);

                sp.Children.Add(sp2);
                detailsInfo.Children.Add(b);
            }
            }
            else
                LayoutRoot.Visibility = Visibility.Collapsed;

        }

        void dcb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var dcb = sender as DictComboBox;
            var detail = dcb.DataContext as JinShengService.JinShen_DangGongWei_Detail;
            var sp = dcb.Tag as StackPanel;
            if (dcb.SelectedValue != null && dcb.SelectedValue.Equals("是"))
            {
                sp.Visibility = System.Windows.Visibility.Visible;
            }
            else if (dcb.SelectedValue != null && dcb.SelectedValue.Equals("否"))
            {
                sp.Visibility = System.Windows.Visibility.Collapsed;
                detail.ShiYongQiYear = 0;
            }
        }

      
    }
}
